package com.jason.fingerprint.store;

import com.jason.fingerprint.beans.ui.FingerPrintBean;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/8/26.
 */
public class FingerXmlParse {

    public static String getNodeString(Element em, String name) {
        try {
            return em.getElementsByTagName(name).item(0).getFirstChild().getNodeValue();
        } catch (Exception e) {

        }
        return "";
    }

    public static ArrayList<FingerPrintBean> parseFingerList(Document doc) {
        ArrayList<FingerPrintBean> beans = new ArrayList<FingerPrintBean>();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Element root = doc.getDocumentElement();
            NodeList items = root.getElementsByTagName("FingerPrintBean");
            if (items.getLength() > 0) {
                for (int i = 0; i < items.getLength(); i++) {
                    FingerPrintBean bean = new FingerPrintBean();
                    Element lin = (Element) items.item(i);
                    bean.id = Integer.valueOf(getNodeString(lin, "id"));
                    bean.userId = getNodeString(lin, "userId");
                    bean.fp = getNodeString(lin, "fp");
                    bean.cardNo = getNodeString(lin, "cardNo");
                    bean.type = Integer.valueOf(getNodeString(lin, "type"));
                    bean.state = Integer.valueOf(getNodeString(lin, "state"));
                    beans.add(bean);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return beans;
    }

    public static ArrayList<FingerPrintBean> parseFingerList(InputStream inputStream) {
        ArrayList<FingerPrintBean> beans = new ArrayList<FingerPrintBean>();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        Document doc = null;
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            doc = builder.parse(inputStream);
            Element root = doc.getDocumentElement();
            NodeList items = root.getElementsByTagName("FingerPrintBean");
            if (items.getLength() > 0) {
                for (int i = 0; i < items.getLength(); i++) {
                    FingerPrintBean bean = new FingerPrintBean();
                    Element lin = (Element) items.item(i);
                    bean.id = Integer.valueOf(getNodeString(lin, "id"));
                    bean.userId = getNodeString(lin, "userId");
                    bean.fp = getNodeString(lin, "fp");
                    bean.cardNo = getNodeString(lin, "cardNo");
                    bean.type = Integer.valueOf(getNodeString(lin, "type"));
                    bean.state = Integer.valueOf(getNodeString(lin, "state"));
                    beans.add(bean);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return beans;
    }

    public static Document parseFingerToXml(List<FingerPrintBean> list) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = dbf.newDocumentBuilder();
        } catch (Exception e) {
        }

        Document doc = builder.newDocument();
        Element root = doc.createElement("FingerPrintBean");
        doc.appendChild(root);

        for (int i = 0; i < list.size(); i++) {
            Element le = doc.createElement("FingerPrintBean");
            root.appendChild(le);

            Element id = doc.createElement("id");
            le.appendChild(id);
            id.appendChild(doc.createTextNode(String.valueOf(list.get(i).id)));

            Element userId = doc.createElement("userId");
            le.appendChild(userId);
            userId.appendChild(doc.createTextNode(list.get(i).userId));

            Element userName = doc.createElement("userName");
            le.appendChild(userName);
            userName.appendChild(doc.createTextNode(list.get(i).userName));

            Element fp = doc.createElement("fp");
            le.appendChild(fp);
            fp.appendChild(doc.createTextNode(list.get(i).fp));

            Element cardNo = doc.createElement("cardNo");
            le.appendChild(cardNo);
            cardNo.appendChild(doc.createTextNode(list.get(i).cardNo));

            Element type = doc.createElement("type");
            le.appendChild(type);
            type.appendChild(doc.createTextNode(String.valueOf(list.get(i).type)));

            Element state = doc.createElement("state");
            le.appendChild(id);
            state.appendChild(doc.createTextNode(String.valueOf(list.get(i).state)));

        }

        return doc;
    }

    //XML File Write
    public static void WriteXmlFile(Document doc, String filename, String codetype) {
        try {
            FileOutputStream fos = new FileOutputStream(filename);
            OutputStreamWriter outwriter = new OutputStreamWriter(fos);
            try {
                Source source = new DOMSource(doc);
                Result result = new StreamResult(outwriter);
                Transformer xformer = TransformerFactory.newInstance().newTransformer();
                xformer.setOutputProperty(OutputKeys.ENCODING, codetype);    //"gb2312" "utf-8"
                xformer.transform(source, result);
            } catch (TransformerConfigurationException e) {
                e.printStackTrace();
            } catch (TransformerException e) {
                e.printStackTrace();
            }
            outwriter.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //XML File Read
    public static Document ReadXmlFile(String filename) {
        FileInputStream fin = null;
        InputStream inStream = null;
        Document doc = null;
        try {
            fin = new FileInputStream(filename);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        inStream = new BufferedInputStream(fin);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            //���ַ�����
            //ByteArrayInputStream is = new ByteArrayInputStream(txt.getBytes());
            doc = builder.parse(inStream);
            //inStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return doc;
    }
}
